package com.moleq.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;

public abstract class MQBaseAdapter extends BaseAdapter {

	protected int selectedRow = -1;
	protected boolean isEditable = false;
	protected MyTextWatcher mMyTextWatcher = new MyTextWatcher();
	private int mEditingResId = -1;

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		isEditable = false;
		return null;
	}

	@Override
	public void notifyDataSetChanged() {
		isEditable = false;
		super.notifyDataSetChanged();
	}
	
	public void ScrollToRow(int row) {
		selectedRow = row;
		this.notifyDataSetChanged();
	}
	
	public int GetSelectedIndex() {
		return selectedRow;
	}

	abstract public void onTextChange(int resId ,String changedText);
	
	protected OnTouchListener mOnTouchListener = new OnTouchListener() {
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_UP) {
				selectedRow = (Integer) v.getTag();
				if (v instanceof EditText) {
					v.setFocusable(true);
					v.setFocusableInTouchMode(true);
					mEditingResId = v.getId();
					if (!isEditable) {
						isEditable = true;
					}
				}
			}
		
			return false;
		}
	};

	class MyTextWatcher implements TextWatcher {

		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if (isEditable) {
				onTextChange(mEditingResId,s.toString());
			}
		}

		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		public void afterTextChanged(Editable s) {
			
		}
	}

}
